package cn.edu.nuc.onlinestore.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.edu.nuc.onlinestore.vo.Goods;
import cn.edu.nuc.onlinestore.vo.Person;



public class Acceptutil extends Thread{
	String a="success";
	String c="difel";
	static int b=-1;
	@Override
	public void run() {
		 
		ServerSocket server = null;
		try {
			server = new ServerSocket(8080);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while( true ){
			synchronized (this) {
				
			
			try {
				//启动一个serverSocket监听8080端口
				
				//阻塞等待客户端连接
				
					
					Socket client = server.accept();
					OutputStream out ;
					DataOutputStream dout;
					InputStream in = client.getInputStream();
					DataInputStream dis = new DataInputStream(in);
					String msg = dis.readUTF();
					
					System.out.println(">>" + msg);
					
					String[] str= msg.split("@");
					System.out.println(str[0]);
						switch(str[0]){
							case "login":userLogin(str);break;
							case "add":	userAdd(str);break;
							case "get":	Send();break;
							case "del":	Del(str[1]);break;
							
						}
					System.out.println(b);	
					if(b==1){
						out = client.getOutputStream();
						dout = new DataOutputStream(out);
						dout.writeBoolean(false);
						System.out.println(b);
						out.close();
						dout.close();
						
//						PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
//						System.out.println("a:" + a);
//						writer.println(a);
//						writer.close();
						
					}else if(b==2){
						out = client.getOutputStream();
						dout = new DataOutputStream(out);
						dout.writeBoolean(true);
						System.out.println(b);
						out.close();
						dout.close();
						
						
					}else if(b==3){
						List<Goods> list=new ArrayList<Goods>();
						File f=new File("D:\\store\\goods\\");
						File[] f1=f.listFiles();
						for(int i=0;i<f1.length;i++){
							GoodsUpdateutil gu=new GoodsUpdateutil();
							Goods g=gu.ObjectUtil_a(f1[i]);
							list.add(g);
						}
						out = client.getOutputStream();
						ObjectOutputStream obj = new ObjectOutputStream(out);
						obj.writeObject(list);
						
						System.out.println(b);
						out.close();
						obj.close();
					}
					in.close();
					dis.close();
				}
						
				
				catch (IOException e) {
				// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
		

	private void Del(String name) {
		File f=new File("D:/store/Online/user/"+name+".db");
		if(f.exists()){
			f.delete();
			b=2;
		}
		
		
	}


	private void Send() {
		b=3;
		
	}

	/*
	 * 线程 接受用户注册信息 并进行判断
	 * 如果不通过  向客户端返回false
	 */
	private void userAdd(String[] str) {
		// TODO Auto-generated method stub
		File f=new File("D:/store/user/"+str[1]+".db");
		if(f.exists()){
			b=1;
		}else{
			FileOutputStream out;
			ObjectOutputStream dout;
			
			try {
				out = new FileOutputStream("D:/store/user/"+str[1]+".db");
				dout=new ObjectOutputStream(out);
				dout.writeObject(new Person(str[1],str[2]));
				b=2;
				out.close();
				dout.close();
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
		
	
	
/*
 * 线程 接受用户登录信息 并进行判断
 * 如果通过 将登录用户写入  在线用户文件表
 */
	private void userLogin(String[] str) {
		// TODO Auto-generated method stub
		
		
		
		FileInputStream in;
		ObjectInputStream din;
		Person p = null;
		File f=new File("D:/store/user/"+str[1]+".db");
		if(f.exists()){
				try {
				   	 in = new FileInputStream(f);
					 din=new ObjectInputStream(in);
					 p=(Person)din.readObject();
					 	if(p.getPassword().equals(str[2])){
									FileOutputStream out;
									ObjectOutputStream dout;
									
									try {
										out = new FileOutputStream("D:/store/Online/user/"+str[1]+".db");
										dout=new ObjectOutputStream(out);
										dout.writeObject(new Person(str[1],str[2]));
										
										out.close();
										dout.close();
										
									} catch (FileNotFoundException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
							b=2;
						}else{
							b=1;
						}
						
						in.close();
						din.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}else{
			b=1;
		}
		
	}
}
